<?php
function myException($exception) {
	Helper_Common::log(print_r(func_get_args(), true), 'api_err');
	$result = array(
		'errcode' => 9999,
		'errmsg' => '服务器发生错误',
		'data' => array()
	);
	$this->logs("RESPONSE:" . print_r($result, true));
	$appcalitonType = isset ($_SERVER ['HTTP_ACCEPT']) ? trim($_SERVER ['HTTP_ACCEPT']) : '';
	echo json_encode($result);
	exit ();
}

require_once Q::ini('app_config/ROOT_DIR') . '/app/helper/wxpayapi.php';
// set_exception_handler ( 'myException' );
// set_error_handler ( 'myException' );

/**
 * 默认控制器
 */
class Controller_wxapp extends Controller_Abstract {


	/**
	 * 客户端代理ID
	 *
	 * @var int
	 */
	protected $acct_id = 0;
	protected $_wxapikey = '';
	protected $openid = '';
	protected $unionid = '';
	protected $appid = "";
	protected $secret = "";
	protected $mch_id = "";
	protected $paykey = "";
	protected $appConfig = array();
	protected $version = 0;
	protected $regKeyConfig = array(
		'notdefined' => 4
	);
	protected $aodian_pubkey = "pub_2ef431a14f6d147e5942b0c7f03bcb6a";
	protected $aodian_subkey = "sub_43254df680e7f58d0334166b6b7ea396";
	protected $aodian_skey = "s_ad912a9cc28543238c237ff43d08a226";
	protected $appErrCode = array(
		'0' => '操作成功',
		'1001' => 'need sign',
		'1002' => 'appid错误',
		'1004' => 'need appid',
		'1003' => '加密错误',
		'1006' => '需要绑定手机号码',
		'1005' => '请求参数不完整',
		'2001' => '密码错误',
		'2002' => '验证码错误',
		'2003' => '密码和确认密码不一致',
		'2004' => '用户未找到',
		'2005' => '用户被锁定',
		'2027' => '今日红包已抽完，请明日再来',
		'2028' => '此功能尚未开启',
		'2025' => '红包金额尚未设置',
		'2026' => '今日抽奖机会已经用完，请明日再来',
		'2029' => '授权码不能为空',
		'2030' => '授权码错误',
		'2031' => '授权码过期',
		'2033' => '数据未找到',
		'2034' => '授权码已经使用',
		'2035' => '该用户已经授权',
		'2037' => '新闻未找到',
		'2039' => '文章未找到',
		'2040' => '您粘贴的不是微信新闻',
		'2041' => 'code参数错误',
		'2042' => 'url格式不正确',
		'2043' => '该手机号码已经注册,请返回登陆界面直接登陆',
		'2044' => '免费试用次数已经使用完，请输入授权码授权',
		'2045' => '重复请求',
		'2046' => '没有数据',
		'3001' => '库存不足',
		'3002' => '无法删除进行中的订单',
		'3003' => '无法取消进行中的订单',
		'3004' => '请设置收货地址',
		'3005' => '商品已下架',
		'4001' => '已经参与评论，不能重复评论',
		'5005' => '内容已下架',
		'6001' => '账户余额不足',
		'6002' => '请先设置提现银行卡',
		'10000' => '未知错误',
		'888888' => '此版本为盗版',

	);

	protected $requestData = array();

	/**
	 *授权校验
	 * @param string $mgs
	 */
	protected function authrize() {
		//检测
		if (!defined("HILL_VERSION")) {
			$this->responseMsg(888888);
		}

		$this->acct_id = $_SERVER['HTTP_ACCTID'];
		$authkey = 'eduauth_' . $this->acct_id;

		$authdir = Q::ini('app_config/ROOT_DIR') . '/tmp/auth';
		if (!is_dir($authdir)) {
			@mkdir($authdir, 0700);
		}
		$authfile = $authdir . '/auth.txt';
		if (!is_file($authfile)) {
			//读取
			$this->appErrCode [1011] .= '请进入管理后台首页点击更新授权';
			$this->responseMsg(1011);
		} else {//已经授权过了 直接读取值进行解密---判断授权合法性以及授权时间
			$authstr = file_get_contents($authfile);
			$config = Q::ini('appini/offerwall');
			if (strlen($config ['key']) > 24)
				$config ['key'] = substr($config ['key'], 0, 24);
			$crypt = new Helper_crypt3des ($config ['key'], $config ['iv']);
			$auth_decodestr = $crypt->decrypt($authstr);
			$auth_array = json_decode($auth_decodestr, true);
			if (is_array($auth_array)) {
				if (empty($auth_array[$this->acct_id])) {
					$this->appErrCode [1011] .= '您的系统还没授权，请联系系统管理员';
					$this->responseMsg(1011);
				} else {
					if (strtotime($auth_array[$this->acct_id]) < strtotime(date('Y-m-d '))) {
						$this->appErrCode [1011] .= '您的授权已经过期，请联系系统管理员';
						$this->responseMsg(1011);
					}
				}
			} else {
				$this->appErrCode [1011] .= '授权信息异常，请进入管理后台首页重新点击更新授权';
				$this->responseMsg(1011);
			}
		}
	}


	function _before_execute() {

		//$this->authrize();
		$filename = Q::ini('app_config/ROOT_DIR') . '/config/api.yaml';
		$this->appConfig = Helper_YAML::load($filename);

		$this->_wxapikey = $this->appConfig['wxapikey'];
		foreach ($_REQUEST as $k => $v) {
			$this->requestData["$k"] = str_replace('(null)', '', $v);
		}
	}

	protected function getuserid($checkphone = true) {


		$filename = Q::ini('app_config/ROOT_DIR') . '/config/redis.yaml';
		$config = Helper_YAML::load($filename);
		$redis = new Redis();
		$redis->pconnect($config['host'], $config['port']);
		$host = Helper_Common::getRequestHost();
		if ($redis->get("edulive:$host:openid_userid:" . $this->openid)) {
			$uid = $redis->get("edulive:$host:openid_userid:" . $this->openid);
			$user = acctuser::find(array("id" => $uid))->getOne();
//			dump($uid);die;
//			dump($user);
			if ($checkphone && !$user->phone) {
				$this->responseMsg(1006);
			}
			if (!$user->id()) {
				$redis->del("edulive:$host:openid_userid:" . $this->openid);
				$this->responseMsg(1003);
			} else {
				return $uid;
			}

		}
		if (!$this->unionid && !$this->openid) {
			$this->responseMsg(1003);
		}

		if ($this->unionid) {
			$user = acctuser::find(array("wechat_union_id" => $this->unionid))->getOne();
			if (!$user->id()) {
				$user = acctuser::find(array("wechat_open_id" => $this->openid))->getOne();
			}
		} else {
			$user = acctuser::find(array("wechat_open_id" => $this->openid))->getOne();
		}

		if (!$user->id()) {
			$this->responseMsg(2004);
		} else {
			$rd_ok = $redis->set("edulive:$host:openid_userid:" . $this->openid, $user->id);
			$redis->expire("edulive:$host:openid_userid:" . $this->openid, 86400);
			//$redis->exprie();
			return $user->id;
		}

	}

	/**
	 * 记录API请求日志
	 *
	 * @param string $mgs
	 */
	protected function logs($msg) {
		$action_log = trim($this->_context->action_name);
		Helper_Common::log($msg, $action_log);
	}

	/**
	 * 输出消息
	 *
	 * @param int $code
	 *            错误码
	 * @param string $msg
	 *            错误信息
	 * @param array $data
	 *            输出数据
	 */
	protected function responseMsg($code, $data = array()) {

		$msg = isset ($this->appErrCode [$code]) ? trim($this->appErrCode [$code]) : '系统错误';
		$result = array(
			'errcode' => intval($code),
			'errmsg' => trim($msg),
			'data' => $data
		);
		$this->logs("RESPONSE:" . print_r($result, true));
		$result = json_encode($result);
		if (strpos($result, '{tel}') !== false) {
			$result = str_replace('{tel}', $this->requestData['username'], $result);
		}
		exit  ($result);


	}


	/**
	 * 请求参数效验
	 *
	 * @param array $questData
	 *            当次请求需要效验的参数键
	 */
	protected function checkParamValid($need_login = true) {
		$this->logs("REQUEST URL:{$_SERVER['REQUEST_URI']},HEADER=" . print_r($_SERVER, true) . ",REQUEST " . $this->_context->action_name . " :" . print_r($_REQUEST, true));

		// if(!empty($_SERVER['HTTP_OPENID']) && !empty($_SERVER['HTTP_RDSESSION']) && !empty($_SERVER['HTTP_ACCTID'])){
		$this->openid = $_SERVER['HTTP_OPENID'];
		$this->rdsession = $_SERVER['HTTP_RDSESSION'];
		$this->acct_id = $_SERVER['HTTP_ACCTID'];
		$this->unionid = $_SERVER['HTTP_UNIONID'];
		$this->version = $_SERVER['HTTP_VERSION'];
		// }

		//验证rdsession是否正常
		if ($need_login) {
			$cacheName = $this->openid;
			$filename = Q::ini('app_config/ROOT_DIR') . '/config/redis.yaml';
			$config = Helper_YAML::load($filename);
			$redis = new Redis();
			$redis->pconnect($config['host'], $config['port']);
			$rd_ok = $redis->get($cacheName);
			if (!empty($rd_ok)) {
				if ($rd_ok != $this->rdsession) {
					$this->responseMsg(1003);
				}
				return true;
			} else {
				$this->responseMsg(1003);
				//未登录或者登录过期
				return false;
			}
		}

	}

	/**
	 * 检查是否登陆
	 * @Author   xy
	 * @DateTime 2019-08-23T09:44:54+0800
	 * @return   bool
	 */
	protected function checkLogin() {
		if ($this->acct_id) {
			$this->checkParamValid(false);
		}
		if ($this->openid) {
			$cacheName = $this->openid;
			$filename = Q::ini('app_config/ROOT_DIR') . '/config/redis.yaml';
			$config = Helper_YAML::load($filename);
			$redis = new Redis();
			$redis->pconnect($config['host'], $config['port']);
			$rd_ok = $redis->get($cacheName);
			if (!empty($rd_ok)) {
				if ($rd_ok == $this->rdsession) {
					return true;
				}

			}
		}
		return false;
	}

	//授权登陆logo
	function actionloginlogo() {
		$this->acct_id = $_SERVER['HTTP_ACCTID'];
		$acct = acct::find("id = " . $this->acct_id)->getOne();
		$this->responseMsg(0, $acct->login_logo);
	}

	//小程序授权登录
	function actionlogin() {

		$code = $this->_context->code;
		if (!$code) {
			$this->responseMsg(1005);
		}

		//获取用户openid
		$url = "https://api.weixin.qq.com/sns/jscode2session?";
		$this->acct_id = $_SERVER['HTTP_ACCTID'];
		$acct = acct::find("id = " . $this->acct_id)->getOne();
		$param = array(
			"appid" => $acct->app_id,
			"secret" => $acct->app_secret,
			"js_code" => $code,
			"grant_type" => "authorization_code"
		);


		$curl = $url . http_build_query($param);
		$resdata = Helper_Common::get_data($curl);
		$res = json_decode($resdata, true);

		if (!array_key_exists('errcode', $res)) {
			$session_key = $res['session_key'];
			$openid = $res['openid'];
			$rdsession = md5($this->_wxapikey . $openid . $session_key);
			//解密数据

			$pc = new Helper_wxbizdatacrypt($acct->app_id, $session_key);
			$errCode = $pc->decryptData($this->_context->encrypted_data, $this->_context->iv, $data);
			$unionid = '';
			//获取unionid
			if ($errCode == 0) {
				$data = json_decode($data, true);
				if (!empty($data['unionId'])) {
					$unionid = $data['unionId'];
				}
			} else {
				$this->logs("解密数据失败" . print_r($errCode, true));
			}
			//创建更新用户信息
			$user_info = json_decode($this->_context->user_info, true);
			$user_id = acctuser::followuser($this->acct_id, $openid, $unionid, $user_info, $this->_context->pid);
			$user_info['user_id'] = $user_id;
			$user_info['phone'] = acctuser::find("id=?", $user_id)->getOne()->phone;
			$user_info['company_id'] = intval(acctuser::find("id=?", $user_id)->getOne()->company_id);
			//openid rdsession写入缓存
			$filename = Q::ini('app_config/ROOT_DIR') . '/config/redis.yaml';
			$config = Helper_YAML::load($filename);
			$redis = new Redis();
			$redis->pconnect($config['host'], $config['port']);
			$rd_ok = $redis->set($openid, $rdsession);
		} else {
			$this->responseMsg(2041);
		}

		//$rdsession 返给前端
		$data = array(
			"session_key" => $session_key,
			"rdsession" => $rdsession,
			"openid" => $openid,
			"unionid" => $unionid,
			"user_info" => $user_info
		);
		$this->responseMsg(0, $data);

	}

	/**
	 * 获取手机号码
	 * @Author   xy
	 * @DateTime 2019-10-24T11:27:28+0800
	 * @return   [type]                   [description]
	 */
	function actionphone() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid(false);
		$acct = acct::find("id = " . $this->acct_id)->getOne();

		//解密数据
		$pc = new Helper_wxbizdatacrypt($acct->app_id, $this->_context->session_key);
		$errCode = $pc->decryptData($this->_context->encrypted_data, $this->_context->iv, $data);

		if ($errCode == 0) {
			$data = json_decode($data, true);
			$user = acctuser::find("id=?", $user_id)->getOne();
			$user->phone = $data['phoneNumber'];
			$user->save();
			$data['uid'] = $user->id;
			$data['phone'] = $user->phone;
			$this->responseMsg(0, $data);
		} else {
			$this->logs("解密数据失败" . print_r($errCode, true));
			$this->responseMsg(1003);
		}


	}

	//小程序首页信息接口
	function actionindex() {
		$signCheck = $this->checkParamValid(false);
		 $user_id = $this->getuserid();
		//轮播图
		$banner = banner::find(array("acct_id" => $this->acct_id, "type" => 0, "status" => 1))
			->order('weight desc')
			->asArray()
			->getAll();
		//菜单
		$menu = personalmodule::getindexmenu($this->acct_id, $this->version);
		//直播课堂
		$live_index = video::indexlivelist($this->acct_id, 0, 3);
		//首页模块
		$index_plate = videoplate::getindexmodule($this->acct_id, $this->version);
		//推荐商品
		$rec_goods = shopgoods::getrecgoods($this->acct_id, 4);
		//直播开关
		$live_auth = acct::checkversion($this->acct_id, $this->version);
		//在线课堂标题
		$live_title = Helper_Common::getPublicSet("首页直播标题", $this->acct_id);
		//商品推荐标题
		$rec_title = Helper_Common::getPublicSet("首页商品推荐标题", $this->acct_id);
		//首页商品推荐简介
		$rec_intro = Helper_Common::getPublicSet("首页商品推荐简介", $this->acct_id);
		$userinfo = acctuser::find('id = ?', $user_id)->asArray()->getOne();
		if($userinfo['company_id']==0||$userinfo['company_id']==''){
			$is_company=0;
		}else{
			$is_company=1;
		}
		$responseData = array(
			"banner" => $banner,
			"menu" => $menu,
			"live_index" => $live_index,
			"index_plate" => $index_plate,
			"rec_goods" => $rec_goods,
			"live_auth" => $live_auth,
			"live_title" => $live_title,
			"rec_title" => $rec_title,
			"rec_intro" => $rec_intro,
			"is_company" => $is_company,
		);
		$this->responseMsg(0, $responseData);
	}

	//首页课程模块更多
	function actionmoreplate() {
		$signCheck = $this->checkParamValid();
		$id = $this->_context->id;
		if (empty($id) || $id == "undefined") {
			$this->responseMsg(1005);
		}
		$res = videoplate::getindexmodulemore($id, $this->version);
		$this->responseMsg(0, $res);
	}

	function actionpingourule() {
		$set = pubconfig::getValue('pintuan_img');
		if ($set) {
			$this->responseMsg(0, $set);
		}
		$url = Helper_Common::getRequestHost();
		$this->responseMsg(0, $url . "/img/pingou.jpg");
	}

	/*
	 * 底部菜单
	 */
	function actiontabbar() {
		$this->acct_id = $_SERVER['HTTP_ACCTID'];
		$res = tabbar::gettabbar($this->acct_id, $_SERVER['HTTP_VERSION']);
		$this->responseMsg(0, $res);
	}

	/*
	 * 个人中心
	 */
	function actionusercenter() {
		$signCheck = $this->checkParamValid(false);
		if ($this->checkLogin()) {
			$user_id = $this->getuserid();
			$collect_num = collect::mycollectnum($user_id);
			$video_num = videoorder::find(array(
				"user_id" => $user_id,
				"is_delete" => 0,
				"is_pay" => 1,
				"refund_status" => 0
			))->getCount();
			$level_id = acctuser::getuserviplevel($user_id);
			$res['viplevel'] = $level_id ? viplevel::getvipname($level_id) : 0;
			$res['collect_num'] = $collect_num;
			$res['video_num'] = $video_num;
		} else {
			$res['viplevel'] = 0;
			$res['collect_num'] = 0;
			$res['video_num'] = 0;
		}

		$is_company_admin = company::find('uid = ? ', $user_id)->getOne();
		if($is_company_admin->id()){
			$res['is_company_admin']=1;
		}else{
			$res['is_company_admin']=0;
		}

		$res['live_auth'] = acct::checkversion($this->acct_id, $this->version);
		$this->responseMsg(0, $res);
	}

	/*
	 * 讲师列表
	 */
	function actionteacherlist() {
		$signCheck = $this->checkParamValid(false);
		$res = teacher::getlist($this->acct_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 讲师详情
	 */
	function actionteacherdetail() {
		$signCheck = $this->checkParamValid(false);
		$id = $this->_context->id;
		if (empty($id)) {
			$this->responseMsg(1005);
		}
		$res = teacher::teacherdetail($id);
		$this->responseMsg(0, $res);
	}

	/*
	 * VIP开通列表
	 */
	function actionviplist() {
		$signCheck = $this->checkParamValid();
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$this->responseMsg(0, []);
		}
		$user_id = $this->getuserid();
		if (acct::checkversion($this->acct_id, $this->version)) {
			$res = viplevel::getviplist($user_id, $this->acct_id);
		} else {
			$res = array();
		}

		$this->responseMsg(0, $res);
	}

	/*
	 * VIP开通下单
	 */
	function actionbuyvip() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$id = $this->_context->id;
		if (empty($id)) {
			$this->responseMsg(1005);
		}
		$res = viporder::createviporder($id, $user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 课程分类
	 */
	function actioneducategory() {
		$signCheck = $this->checkParamValid(false);
		$category = educategory::getparentlist($this->acct_id);
		$category[0] = '全部';
		$this->responseMsg(0, $category);
	}

	/*
	 * 课程分类
	 */
	function actioneducategoryselect() {
		$signCheck = $this->checkParamValid(false);
		$category = educategory::getselectparentlist($this->acct_id);
		array_push($category, array("id" => 0, "name" => "其它"));
		$this->responseMsg(0, $category);
	}

	/*
	 * 视频搜索
	 */
	function actionsearchvideo() {
		$signCheck = $this->checkParamValid(false);
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$this->responseMsg(0, []);
		}
		$key = trim($this->_context->key);
		if (empty($key)) {
			$this->responseMsg(0);
		} else {
			$res = edusearch::search($this->acct_id, $key, $this->version);
			$this->responseMsg(0, $res);
		}
	}

	/*
	 * 分类课程列表
	 */
	function actionclassvideo() {
		$signCheck = $this->checkParamValid(false);
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$this->responseMsg(0, []);
		}
		$class_id = $this->_context->class_id;
		$res = video::classvideolist($this->acct_id, $class_id, $this->version);
		$user_id = $this->getuserid();
		for ($i = 0; $i < count($res); $i++) {
			$learnschedule = learnschedule::find('uid = ? AND video_id = ?', $user_id, $res[$i]['id'])->asArray()->getAll();
			if (empty($learnschedule)) {
				$res[$i]['learnschedule'] = 0;
			} else {
				$res[$i]['learnschedule'] = intval($learnschedule[0]['schedule']*100);
			}
			$res[$i]['id'] = (int)$res[$i]['id'];
		}

		for ($i1 = 0; $i1 < count($res); $i1++) {
			$videoexam=videoexam::find('video_id = ? and user_id = ?',$res[$i1]['id'],$user_id)->asArray()->getAll();
			if(!empty($videoexam)){
				$videochapter=videochapter::find("id = ?",$videoexam[0]['chapter_id'])->asArray()->getAll();
				$res[$i1]['videochapter_name']=$videochapter[0]['name'];
			}else{
				$res[$i1]['videochapter_name']='';
			}
		}
		$this->responseMsg(0, $res);
	}

	/*
	 * 拼团课程列表
	 */
	function actionpintuanvideo() {
		$signCheck = $this->checkParamValid(false);
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$this->responseMsg(0, []);
		}
		$res = video::pintuanvideolist($this->acct_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 获取视频详情
	 */
	function actionvideo() {
		$signCheck = $this->checkParamValid();
		$video_id = $this->_context->video_id;
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$this->responseMsg(5005);
		}
		if (empty($video_id) || $video_id == 'undefined') {
			$this->responseMsg(5005);
		}
		$for_video_id = $this->_context->for_video_id;
		$password = $this->_context->password;
		$user_id = $this->getuserid();
		$check = video::find("id = ?", $video_id)->getOne();
		if ($check->id()) {
			$video = video::getvideodetail($video_id, $user_id, $password, $for_video_id);
			if (!$video['live_url'] && $video['teacher_id']) {
				$teacher = teacher::find('id = ? ', $check->teacher_id)->getOne();
				if ($teacher->id()) {
					$video['live_url'] = $teacher->playurl;
				}
			}
			$zhubo = teacher::find('id = ? and user_id = ?', $check->teacher_id, $user_id)->getOne();
			if ($zhubo->id()) {
				$video['iszhubo'] = 1;
			} else {
				$video['iszhubo'] = 0;
			}
			$acct = acct::find('id=?', $this->acct_id)->getOne();
			$video['app_id'] = $acct->app_id;
			$video['pubkey'] = $acct->aodian_accessid;
			$video['subkey'] = $acct->aodian_accesskey;
			//$api = new Helper_TisApi($acct->aodian_accessid,$acct->aodian_accesskey,$acct->aodian_auth);
			$this->responseMsg(0, $video);
		} else {
			$this->responseMsg(5005);
		}


	}


	/*
	 * 创建开团
	 */
	function actioncreatepintuan() {
		$signCheck = $this->checkParamValid();
		$video_id = $this->_context->video_id;
		$pintuan_id = $this->_context->pintuan_id;
		$pintuan_info = videopintuan::find("id = ?", $pintuan_id)->getOne();
		$video_info = video::find("id = ?", $video_id)->getOne();
		$user_info = acctuser::getuserinfobyopenid($this->openid);
		$user_id = $user_info->id;
		$type = 1;
		if ($video_info->style == 3) {
			$type = 4;
		} elseif ($video_info->style == 0) {
			$type = 1;
		}
		$join_data = array(array("user_id" => $user_id, "add_time" => time()));
		//开团
		$ptdetail_data = array(
			"acct_id" => $this->acct_id,
			"pintuan_id" => $pintuan_id,
			"kaituan_uid" => $user_id,
			"join_num" => 1,
			"need_num" => $pintuan_info->person_num,
			"video_id" => $video_id,
			"join_detail" => json_encode($join_data)
		);
		$pintuandetail = new videopintuandetail($ptdetail_data);
		$pintuandetail->save();
		$detail_id = $pintuandetail->id;
		//子订单
		$order_data = array(
			"acct_id" => $this->acct_id,
			"order_no" => coreorder::getorderno(),
			"video_id" => $video_id,
			"pintuan_id" => $pintuan_id,
			"pintuan_detailid" => $detail_id,
			"issoure" => 1,
			"user_id" => $user_id,
			"price" => $pintuan_info->price,
			"type" => $type
		);

		$order = new videoorder($order_data);
		$order->save();
		$order_id = $order->id;
		// videopintuandetail::createpintuan($order_id);
		//主订单
		$core_data = array(
			"acct_id" => $this->acct_id,
			"user_id" => $user_id,
			"ordersn" => $order->order_no,
			"orderid" => $order_id,
			"module_mark" => "video",
			"fee" => $order->price
		);
		$core = new coreorder($core_data);
		$core->save();
		$core_id = $core->id;
		$res = array("order_id" => $core_id, "detail_id" => $detail_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 参加拼团
	 */
	function actionjoinpintuan() {
		$signCheck = $this->checkParamValid();
		//$video_id = $this->_context->video_id;

		$detail_id = $this->_context->detail_id;
		$detail_info = videopintuandetail::find("id = " . $detail_id)->getOne();


		//判断参团是否满员
		if ($detail_info->join_num >= $detail_info->need_num) {
			$this->responseMsg(30, "拼团已经人满了");
		}

		$pintuan_info = videopintuan::find("id = ?", $detail_info->pintuan_id)->getOne();
		//判断拼团时间
		if ($pintuan_info->end_time <= time()) {
			$this->responseMsg(31, "拼团已经截止");
		}

		$video_info = video::find("id = ?", $detail_info->video_id)->getOne();
		$user_info = acctuser::getuserinfobyopenid($this->openid);
		$user_id = $user_info->id;
		$type = 1;
		if ($video_info->style == 3) {
			$type = 4;
		} elseif ($video_info->style == 0) {
			$type = 1;
		}
		//创建订单
		$order_data = array(
			"acct_id" => $this->acct_id,
			"order_no" => coreorder::getorderno(),
			"video_id" => $detail_info->video_id,
			"pintuan_id" => $detail_info->pintuan_id,
			"pintuan_detailid" => $detail_id,
			"issoure" => 0,
			"user_id" => $user_id,
			"price" => $pintuan_info->price,
			"type" => $type
		);

		$order = new videoorder($order_data);
		$order->save();
		$order_id = $order->id;

		$core_data = array(
			"acct_id" => $this->acct_id,
			"ordersn" => $order->order_no,
			"user_id" => $user_id,
			"orderid" => $order_id,
			"module_mark" => "video",
			"fee" => $order->price
		);
		$core = new coreorder($core_data);
		$core->save();
		$core_id = $core->id;
		$this->responseMsg(0, $core_id);
	}

	/*
	 * 拼团详情
	 */
	function actionvideopintuandetail() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$detail_id = $this->_context->detail_id;
		$data = videopintuan::getpintuandetailinfo($detail_id, $user_id);
		$this->responseMsg(0, $data);
	}

	/*
	 * 获取开团列表
	 */
	function actioncreatepintuanlist() {
		$signCheck = $this->checkParamValid();
		$video_id = $this->_context->video_id;
		$data = videopintuan::getvideopintuan($video_id);
		$this->responseMsg(0, $data);
	}

	/*
	 * 获取可参加列表
	 */
	function actionjoinpintuanlist() {
		$signCheck = $this->checkParamValid();
		$video_id = $this->_context->video_id;
		$uid = $this->getuserid();
		$data = videopintuan::getjionpintuan($video_id, $uid);
		$this->responseMsg(0, $data);
	}

	/*
	 * 创建打赏订单
	 */
	function actionaddreward() {
		$signCheck = $this->checkParamValid();

		$order_no = coreorder::getorderno();
		$price = $this->_context->price;
		//打赏订单创建
		$reward_data = array(
			'acct_id' => $this->acct_id,
			'video_id' => $this->_context->video_id,
			'user_id' => $this->getuserid(),
			'order_no' => $order_no,
			'money' => $price
		);
		$o = new videoreward($reward_data);
		$o->save();

		$data = array(
			'orderid' => $o->id,
			'acct_id' => $this->acct_id,
			'fee' => $price,
			'ordersn' => $order_no,
			'user_id' => $this->getuserid(),
			'module_mark' => "reward",
		);
		$core = new coreorder($data);
		$core->save();

		$res = array("order_id" => $core->id, "order_sn" => $order_no);
		$this->responseMsg(0, $res);
	}

	/*
	 * 查询打赏列表
	 */
	function actionreward() {
		$signCheck = $this->checkParamValid();
		$res = videoreward::getrewardlist($this->_context->video_id, $this->_context->page, $this->_context->limit);
		$this->responseMsg(0, $res);
	}

	/*
	 * 添加视频课程评论
	 */
	function actionaddcomment() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$content = trim($this->_context->content);
		$img = $this->_context->img;
		if (empty($this->_context->video_id)) {
			$this->responseMsg(1005);
		}
		if (!empty($img)) {
			$img = json_encode(explode(",", $img));
		}
		$comment = comment::find(array("user_id" => $user_id, "video_id" => $this->_context->video_id))->getOne();
		if ($comment->id()) {
			$this->responseMsg(4001);
		}
		$data = array(
			"acct_id" => $this->acct_id,
			"video_id" => $this->_context->video_id,
			"user_id" => $user_id,
			"content" => $content,
			"upload_img" => $img
		);
		$comment = new comment($data);
		$comment->save();

		$this->responseMsg(0, $comment->id);
	}


	/*
	 * 评论留言列表
	 */
	function actioncommentlist() {
		$signCheck = $this->checkParamValid(false);
		//判断是否分页
		$page = $this->_context->page;
		$limit = $this->_context->limit ? $this->_context->limit : 10;
		if ($page) {
			$comment = comment::getcommentlist($this->_context->video_id, true, $page, $limit);
		} else {
			$comment = comment::getcommentlist($this->_context->video_id, false);
		}
		$this->responseMsg(0, $comment);
	}

	/*
	 * LIVE聊天列表
	 */
	function actionlivecommentlist() {
		$signCheck = $this->checkParamValid();
		// $limit = $this->_context->limit?$this->_context->limit:10;
		$comment = comment::getlivecommentlist($this->_context->video_id);
		$this->responseMsg(0, $comment);
	}

	/*
	 * 提交视频直播购买订单
	 */
	function actionbuyvideo() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$order_no = coreorder::getorderno();
		$video = video::find(array("id" => $this->_context->video_id))->getOne();
		$type = 1;
		//判断媒体类型
		if ($video->style == 0) {
			$type = 1;
		} elseif ($video->style == 3) {
			$type = 4;
		}
		$data = array(
			'acct_id' => $this->acct_id,
			'video_id' => $this->_context->video_id,
			'user_id' => $user_id,
			'order_no' => $order_no,
			'type' => $type,
			'price' => $video->present_price
		);
		$o = new videoorder($data);
		$o->save();

		$data = array(
			'orderid' => $o->id,
			'acct_id' => $this->acct_id,
			'user_id' => $user_id,
			'fee' => $video->present_price,
			'ordersn' => $order_no,
			'module_mark' => "video",
		);
		$core = new coreorder($data);
		$core->save();

		$res = array("order_id" => $core->id, "order_sn" => $order_no);
		$this->responseMsg(0, $res);
	}

	//预约直播
	function actionordervideo() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$order_no = coreorder::getorderno();
		$video = video::find(array("id" => $this->_context->video_id))->getOne();
		if ($this->_context->form_id) {
			$form_id = $this->_context->form_id;
		} else {
			$form_id = "";
		}
		//查询是否已经预约
		$order = videoorder::find(array('user_id' => $user_id, "video_id" => $this->_context->video_id, "type" => 0, "status" => 1))->getOne();
		if ($order->id) {
			//更新form_id
			if ($form_id) {
				$order->form_id = $form_id;
				$order->save();
			}

			$res = array("need_pay" => 0, "msg" => '已经预约');
			$this->responseMsg(0, $res);
		}

		//预约金额大于0需要支付
		if ($video->money > 0) {

			$data = array(
				'acct_id' => $this->acct_id,
				'video_id' => $this->_context->video_id,
				'user_id' => $user_id,
				'order_no' => $order_no,
				'type' => 0,
				'price' => $video->money,
				"form_id" => $form_id,
			);
			$o = new videoorder($data);
			$o->save();

			$data = array(
				'orderid' => $o->id,
				'acct_id' => $this->acct_id,
				'user_id' => $user_id,
				'fee' => $video->money,
				'ordersn' => $order_no,
				'module_mark' => "video",
			);
			$core = new coreorder($data);
			$core->save();

			$res = array("need_pay" => 1, "order_id" => $core->id, "order_sn" => $order_no);
			$this->responseMsg(0, $res);
		} else {
			//无需支付仅创建视频订单同时修改状态
			$data = array(
				'acct_id' => $this->acct_id,
				'video_id' => $this->_context->video_id,
				'user_id' => $user_id,
				'order_no' => $order_no,
				'type' => 0,
				'price' => $video->money,
				'is_pay' => 1,
				'status' => 1,
				"form_id" => $form_id,
			);
			$o = new videoorder($data);
			$o->save();
			$res = array("need_pay" => 0, "msg" => '预约成功');
			$this->responseMsg(0, $res);
		}

	}

	//我的预约
	function actionmyordervideo() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = videoorder::myordervideo($user_id);
		$this->responseMsg(0, $res);
	}

	//我的课程
	function actionmyclass() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = videoorder::myclass($user_id);
		$this->responseMsg(0, $res);
	}

	//我的拼团课程
	function actionmypintuanclass() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = videoorder::mypintuanclass($user_id);
		$this->responseMsg(0, $res);
	}

	//全部直播-近期直播
	function actionlivelist() {
		$signCheck = $this->checkParamValid();
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$this->responseMsg(0);
		}
		$user_id = $this->getuserid();
		$res = video::livelist($this->acct_id, $user_id);
		$this->responseMsg(0, $res);
	}


	//订单调起支付
	function actionpayorder() {
		$signCheck = $this->checkParamValid();
		$order_id = trim($this->_context->order_id);
		if (!$order_id) {
			echo json_encode(array('status' => 2, 'msg' => '支付信息错误！'));
			exit();
		}

		$order_info = coreorder::find('id = ' . $order_id)->asArray()->getOne();
		if (empty($order_info['id'])) {
			echo json_encode(array('status' => 3, 'msg' => '没有找到支付订单！'));
			exit();
		}

		//①、获取用户openid
		if (!$this->openid) {
			echo json_encode(array('status' => 1, 'msg' => '用户状态异常！'));
			exit();
		}
		$data = coreorder::payorder($order_id, $this->openid, $this->acct_id);
		if (!empty($data['return_code'])) {
			if ($data['return_code'] == 'FAIL') {
				echo json_encode(array('status' => 1, 'msg' => $data['return_msg']));
			}
		}
		//②、统一下单
		$this->responseMsg(0, $data);
		exit();
	}

	/*
	 * 视频收藏/取消收藏
	 */
	function actioncollect() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$collect = collect::find(array(
			"acct_id" => $this->acct_id,
			"video_id" => $this->_context->video_id,
			"user_id" => $user_id
		))->getOne();
		//添加收藏
		if (empty($collect->id)) {
			$data = array(
				"acct_id" => $this->acct_id,
				"video_id" => $this->_context->video_id,
				"user_id" => $user_id
			);
			$co = new collect($data);
			$co->save();

		} else {
			//添加收藏
			if ($collect->is_delete == 1) {
				$collect->is_delete = 0;
				$collect->save();
			} else {
				//取消收藏
				$collect->is_delete = 1;
				$collect->save();
			}
		}
		$this->responseMsg(0);
	}

	/*
	 * 我的视频收藏
	 */
	function actionmyvideocollect() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = collect::mycollect($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 活动报名列表
	 */
	function actionactivitylist() {
		$signCheck = $this->checkParamValid(false);
		$res = activitysignup::activitylist($this->acct_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 活动报名详情
	 */
	function actionactivitydetail() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$id = $this->_context->id;
		$res = activitysignup::activitydetail($id, $user_id);
		$this->responseMsg(0, $res);
	}

	//活动报名自定义输入
	function actiongetactinput() {
		$signCheck = $this->checkParamValid();
		$id = $this->_context->activity_id;
		$res = activitysignupset::getSet($id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 参加活动/创建活动订单
	 */
	function actionjoinactivity() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$id = $this->_context->id;
		$save['realname'] = $this->_context->realname;
		$save['mobile'] = $this->_context->tel;
		$other = $this->_context->detail;
		if ($other) {
			$other = json_decode($other, true);
		}

		$data = array();
		//检查输入
		$set = activitysignupset::getSet($id);

		foreach ($set as $key => $value) {
			if ($value['is_must'] == 1) {
				$name = $value['name'];
				if (empty($other) || empty($other[$name])) {
					$this->responseMsg(1005);
				}
			}
			$tmp['type'] = $value['type'];
			$tmp['key'] = $name;
			$tmp['value'] = $other[$name];
			array_push($data, $tmp);
		}

		$save['other_data'] = json_encode($data);
		$res = activitysignup::joinactivity($user_id, $id, $save);
		$this->responseMsg(0, $res);
	}

	/*
	 * 分销中心首页
	 */
	function actionfxcenter() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$userinfo = acctuser::find("id = " . $user_id)->setColumns('id,nickname,avatar_url,commission,used_commission,parentid,viplevel_id,fx_grade')->asArray()->getOne();
		$parentinfo = acctuser::find("id = " . $userinfo['parentid'])->setColumns('nickname')->getOne();
		$viplevel_id = acctuser::getuserviplevel($user_id);
		//$vipinfo = viplevel::find("id = ".$userinfo['viplevel_id'])->setColumns('title')->getOne();
		$agent = fxlevelset::find(array("grade" => $userinfo['fx_grade']))->getOne();
		shoporder::autoconfirmorder();
		$userinfo['parentname'] = $parentinfo->nickname;
		$userinfo['vipname'] = $agent->id() ? $agent->level_name : '普通用户';
		$list = acctuser::getfenxiao($user_id);
		$res['user'] = $userinfo;
		$res['list'] = $list;
		$this->responseMsg(0, $res);
	}

	/*
	 * 用户信息
	 */
	function actionuserinfo() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$userinfo = acctuser::find("id = " . $user_id)->asArray()->getOne();
		$this->responseMsg(0, $userinfo);
	}

	/*
	 * 提现申请
	 */
	function actionsubmitwithdraw() {
		$signCheck = $this->checkParamValid();
		$user_info = acctuser::getuserinfobyopenid($this->openid);
		$money = $this->_context->money;
		//余额不足
		if ($user_info['commission'] < $money) {
			$this->responseMsg(6001);
		}
		//没有绑定银行卡
		$bindpayment_id = bankconfig::getbankid($user_info['id']);
		if ($bindpayment_id == 0) {
			$this->responseMsg(6002);
		}
		$res = withdraw::addwithdraw($user_info['id'], $money, $bindpayment_id);

		$this->responseMsg(0, $res);
	}

	/*
	 * 提现明细列表
	 */
	function actionwithdrawlist() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		// 分页
		// $status = $this->_context->status;
		// $page = $this->_context->page?$this->_context->page:1;
		// $limit = $this->_context->limit?$this->_context->limit:10;
		// $res = fxwithdraw::getlist($user_id,$status,$page,$limit);
		$in = fxwithdraw::getlist($user_id, 0);
		$no = fxwithdraw::getlist($user_id, 2);
		$yes = fxwithdraw::getlist($user_id, 99);
		$res['in'] = $in;
		$res['no'] = $no;
		$res['yes'] = $yes;
		$this->responseMsg(0, $res);
	}


	/*
	 * 佣金明细列表
	 */
	function actioncommissionlist() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$page = $this->_context->page ? $this->_context->page : 1;
		$limit = $this->_context->limit ? $this->_context->limit : 10;
		$res = fxcommission::getlist($user_id, $page, $limit);
		$this->responseMsg(0, $res);
	}

	/*
	 * 下级列表
	 */
	function actionmyteam() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = acctuser::getmyteam($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 添加/修改银行卡
	 */
	function actionaddbankcard() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		if ($this->_context->isPOST()) {
			if ($this->_context->id) {
				$bankconfig = bankconfig::find("id = " . $this->_context->id)->getOne();
				if ($this->_context->bank_account) {
					$bankconfig->bank_account = $this->_context->bank_account;
				}
				if ($this->_context->bank_openadd) {
					$bankconfig->bank_openadd = $this->_context->bank_openadd;
				}
				if ($this->_context->bank_card) {
					$bankconfig->bank_card = $this->_context->bank_card;
				}
				if ($this->_context->is_default) {
					$bankconfig->is_default = $this->_context->is_default;
				}
				if ($this->_context->is_delete) {
					$bankconfig->is_delete = $this->_context->is_delete;
				}

				$bankconfig->save();
				$this->responseMsg(0, $bankconfig->id);
			} else {
				$data = array(
					"bank_account" => $this->_context->bank_account,
					"bank_openadd" => $this->_context->bank_openadd,
					"bank_card" => $this->_context->bank_card,
					"is_default" => $this->_context->is_default,
					"user_id" => $user_id
				);
				$bankconfig = new bankconfig($data);

				$bankconfig->save();
				$this->responseMsg(0, $bankconfig->id);
			}
		}
	}

	/*
	 * 获取银行卡
	 */
	function actionmybankcard() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = bankconfig::getbanklist($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 获取默认银行卡
	 */
	function actionmydefaultcard() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$def = bankconfig::find(array("user_id" => $user_id, "is_default" => 1, "is_delete" => 0))->getOne();
		$count = bankconfig::find(array("user_id" => $user_id, "is_delete" => 0))->getCount('id');
		$res['def_id'] = $def->id;
		$res['card_sum'] = $count;
		$this->responseMsg(0, $res);
	}

	/*
	 * 获取银行卡信息
	 */
	function actioncardinfo() {
		$signCheck = $this->checkParamValid();
		$res = bankconfig::find(array("id" => $this->_context->card_id))->asArray()->getOne();
		$this->responseMsg(0, $res);
	}


	/*
	 * 商城首页
	 */
	function actionshopindex() {
		$signCheck = $this->checkParamValid(false);
		$banner = banner::find(
			array(
				"acct_id" => $this->acct_id,
				"status" => 1,
				"type" => 1
			))
			->asArray()
			->getAll();
		//获取商品分类
		$category = shopcategory::getindexshow($this->acct_id);
		$res['banner'] = $banner;
		$res['category'] = $category;
		$this->responseMsg(0, $res);
	}

	/*
	 * 商品详情
	 */
	function actiongoodsdetail() {
		$signCheck = $this->checkParamValid(false);

		$goods_id = $this->_context->goods_id;
		//获取商品详情
		$goods_info = shopgoods::getgoodsinfo($goods_id);
		if (empty($goods_info['id'])) {
			$this->responseMsg(3005);
		} elseif ($goods_info['is_show'] == 0) {
			$this->responseMsg(3005);
		}
		//获取服务信息
		if ($goods_info['serviceids']) {
			$service_arr = explode(',', $goods_info['serviceids']);

			//$tmp =array();
			for ($i = 0; $i < count($service_arr); $i++) {
				$goods_info['service_info'][$i] = goodsservice::find("id = " . intval($service_arr[$i]))->asArray()->getOne();
			}
		}

		//$goods_info['service_info'] =$tmp;
		//获取规格信息
		$option = goodsoption::find(array("goodsid" => $goods_id))->asArray()->getAll();
		$goods_info['goodsoption'] = $option;
		//购物车数量
		if ($this->checkLogin()) {
			$user_id = $this->getuserid();
			$goods_info['cart_num'] = shopcart::mycartnum($user_id);
		} else {
			$goods_info['cart_num'] = 0;
		}

		$this->responseMsg(0, $goods_info);
	}

	/*
	 * 商品推荐
	 */
	function actiongoodsrec() {
		$signCheck = $this->checkParamValid(false);

		//商品相关
		$goods_id = $this->_context->goods_id;
		if (empty($goods_id) || $goods_id == 'undifined') {
			$goods_id = null;
		}
		$num = $this->_context->num;
		if (empty($num) || $num == 'undifined') {
			$num = 4;
		}
		//购物车相关
		$res = shopgoods::getrecgoods($this->acct_id, $num, $goods_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 商品搜索
	 */
	function actionsearchgoods() {
		$signCheck = $this->checkParamValid(false);
		$key = trim($this->_context->key);
		if (empty($key)) {
			$this->responseMsg(0);
		} else {
			$res = shopsearch::search($this->acct_id, $key);
			$this->responseMsg(0, $res);
		}
	}

	/*
	 * 分类商品
	 */
	function actioncategorygoods() {
		$signCheck = $this->checkParamValid(false);
		//商品类别
		$category_id = $this->_context->category_id;
		$goods = shopgoods::getcategorygoods($this->acct_id, $category_id);
		if (!empty($category_id)) {
			$info = shopcategory::getcategoryinfo($category_id);
			$res['info'] = $info;
		}
		$res['goods'] = $goods;
		$this->responseMsg(0, $res);
	}

	/*
	 * 订单商品信息
	 */
	function actionordergoodsinfo() {
		$signCheck = $this->checkParamValid();
		$ordergoods_id = $this->_context->ordergoods_id;
		//获取商品详情
		$res = ordergoods::ordergoodsinfo($ordergoods_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 加入购物车
	 */
	function actionaddshopcart() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$goods_id = $this->_context->goods_id;
		$optionid = $this->_context->optionid;
		$total = $this->_context->total;
		$res = shopcart::addcart($this->acct_id, $user_id, $goods_id, $optionid, $total);
		$this->responseMsg(0, $res);
	}

	/*
	 * 我的购物车
	 */
	function actionmycart() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = shopcart::mycart($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 修改购物车
	 */
	function actioneditcart() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$cart_id = $this->_context->id;
		$num = $this->_context->num;
		$cart = shopcart::find("id = " . $cart_id)->getOne();
		$cart->total = $num;
		$cart->save();
		$res = $cart->id;
		$this->responseMsg(0, $res);
	}

	/*
	 * 删除购物车
	 */
	function actiondelcart() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$array = explode(",", $this->_context->ids);
		$this->union_table = shopcart::meta();
		QDB::getConn()->startTrans();
		try {
			$this->union_table->deleteWhere($array);
			QDB::getConn()->completeTrans(true);
		} catch (Exception $e) {
			QDB::getConn()->completeTrans(FALSE);
			$this->responseMsg(10000);
		}
		$this->responseMsg(0, 1);
	}

	/*
	 * 获取支付页面信息
	 */
	function actionshoporderinfo() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();

		//获取商品信息
		$num = intval($this->_context->num);
		$total_price = 0;
		if (!empty($this->_context->goods_id)) {
			//单商品购买
			$goods_id = $this->_context->goods_id;
			$goods_info = shopgoods::getgoodsinfo($goods_id);
			$info['thumb'] = $goods_info['product_thumb'];
			$info['title'] = $goods_info['name'];
			$info['price'] = $goods_info['productprice'];
			$info['num'] = $num;
			$info['dec'] = $goods_info['dec'];
			$total_price += $info['num'] * $info['price'];
			$data[0] = $info;
		} elseif (!empty($this->_context->option_id)) {
			//多规格购买
			$option_id = $this->_context->option_id;
			$option_info = goodsoption::find("id = " . $option_id)->getOne();
			$goods_info = shopgoods::getgoodsinfo($option_info->goodsid);
			$info['thumb'] = $option_info->thumb;
			$info['title'] = $goods_info['name'];
			$info['price'] = $option_info->productprice;
			$info['num'] = $num;
			$info['dec'] = $option_info->title;
			$total_price += $info['num'] * $info['price'];
			$data[0] = $info;
		} elseif (!empty($this->_context->cart_ids)) {
			//购物车购买
			$data = array();
			$i = 0;
			$cart_ids = explode(",", $this->_context->cart_ids);
			foreach ($cart_ids as $value) {
				$cart_info = shopcart::find("id = " . $value)->getOne();
				$goods_info = shopgoods::getgoodsinfo($cart_info->goods_id);
				if ($cart_info->optionid > 0) {
					$option_info = goodsoption::find("id = " . $cart_info->optionid)->getOne();
					$data[$i]['thumb'] = $option_info->thumb;
					$data[$i]['title'] = $goods_info['name'];
					$data[$i]['price'] = $option_info->productprice;
					$data[$i]['num'] = $cart_info->total;
					$data[$i]['dec'] = $option_info->title;
					$data[$i]['stock'] = $option_info->stock;
				} else {
					$data[$i]['thumb'] = $goods_info['product_thumb'];
					$data[$i]['title'] = $goods_info['name'];
					$data[$i]['price'] = $goods_info['productprice'];
					$data[$i]['num'] = $cart_info->total;
					$data[$i]['dec'] = $goods_info['dec'];
					$data[$i]['stock'] = $goods_info['stock'];
				}
				$total_price += $data[$i]['num'] * $data[$i]['price'];
				$i++;
			}
		}

		//是否设置地址
		$is_address = shopaddress::find(array("user_id" => $user_id))->getCount();
		//获取地址信息
		$address = array();
		if ($is_address > 0) {
			$address = shopaddress::defaultaddress($user_id);
		}
		//获取运费
		$shipfee = shipfee::getshipfee($this->acct_id, $total_price);
		$res['goods'] = $data;
		$res['address'] = $address;
		$res['is_address'] = $is_address;
		$res['total_price'] = floatval($total_price);
		$res['shipfee'] = floatval($shipfee);
		$this->responseMsg(0, $res);
	}

	/*
	 * 创建订单
	 */
	function actionaddshoporder() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		//判断收货地址
		if (empty($this->_context->address_id) || $this->_context->address_id == 'undefined') {
			$this->responseMsg(3004);
		}
		//获取商品信息
		$num = intval($this->_context->num);
		$total_price = 0;
		if (!empty($this->_context->goods_id)) {
			//单商品购买
			$goods_id = $this->_context->goods_id;
			$goods_info = shopgoods::getgoodsinfo($goods_id);
			if (empty($goods_info['id'])) {
				$this->responseMsg(3005);
			} elseif ($goods_info['is_show'] == 0) {
				$this->responseMsg(3005);
			}
			//判断库存
			if ($goods_info['stock'] < $num) {
				$this->responseMsg(3001);
			}

			$info['title'] = $goods_info['name'];
			$info['price'] = $goods_info['productprice'];
			$info['num'] = $num;
			$info['goods_id'] = $goods_id;
			$total_price += $info['num'] * $info['price'];
			$data[0] = $info;
		} elseif (!empty($this->_context->option_id)) {
			//多规格购买
			$option_id = $this->_context->option_id;
			$option_info = goodsoption::find("id = " . $option_id)->getOne();
			$goods_info = shopgoods::getgoodsinfo($option_info->goodsid);
			if (empty($goods_info['id'])) {
				$this->responseMsg(3005);
			} elseif ($goods_info['is_show'] == 0) {
				$this->responseMsg(3005);
			}
			if ($option_info['stock'] < $num) {
				$this->responseMsg(3001);
			}
			$info['goods_id'] = $option_info->goodsid;
			$info['option_id'] = $option_id;
			$info['price'] = $option_info->productprice;
			$info['num'] = $num;
			$info['option_name'] = $option_info->title;
			$total_price += $info['num'] * $info['price'];
			$data[0] = $info;
		} elseif (!empty($this->_context->cart_ids)) {
			//购物车购买
			$i = 0;
			$cart_ids = explode(",", $this->_context->cart_ids);
			foreach ($cart_ids as $value) {
				$cart_info = shopcart::find("id = " . $value)->getOne();
				$goods_info = shopgoods::getgoodsinfo($cart_info->goods_id);
				if (empty($goods_info['id'])) {
					$this->responseMsg(3005);
				} elseif ($goods_info['is_show'] == 0) {
					$this->responseMsg(3005);
				}
				if ($cart_info->optionid > 0) {
					$option_info = goodsoption::find("id = " . $cart_info->optionid)->getOne();
					if ($option_info->stock < $num) {
						$this->responseMsg(3001);
					}
					$data[$i]['option_name'] = $goods_info['name'];
					$data[$i]['goods_id'] = $cart_info->goods_id;
					$data[$i]['option_id'] = $cart_info->optionid;
					$data[$i]['price'] = $option_info->productprice;
					$data[$i]['num'] = $cart_info->total;

				} else {
					if ($goods_info['stock'] < $num) {
						$this->responseMsg(3001);
					}
					$data[$i]['goods_id'] = $cart_info->goods_id;
					$data[$i]['price'] = $goods_info['productprice'];
					$data[$i]['num'] = $cart_info->total;
				}
				$num += $data[$i]['num'];
				$total_price += $data[$i]['num'] * $data[$i]['price'];
				$i++;
			}
		}


		//获取地址信息
		$address = shopaddress::find("id = " . $this->_context->address_id)->getOne();
		//获取运费
		$shipfee = shipfee::getshipfee($this->acct_id, $total_price);
		//创建订单
		$order_no = coreorder::getorderno();
		$order_data = array(
			"total" => $num,
			"ordersn" => $order_no,
			"acct_id" => $this->acct_id,
			"user_id" => $user_id,
			"total_fee" => $total_price,
			"ship_fee" => $shipfee,
			"pay_fee" => $total_price + $shipfee,
			"address_id" => $this->_context->address_id,
			"address_detail" => $address->province . $address->city . $address->area . $address->address,
		);
		$shoporder = new shoporder($order_data);
		$shoporder->save();
		$order_id = $shoporder->id;
		//创建core订单
		$coredata = array(
			'orderid' => $order_id,
			'acct_id' => $this->acct_id,
			"user_id" => $user_id,
			'fee' => $total_price + $shipfee,
			'ordersn' => $order_no,
			'module_mark' => "shop",
		);
		$core = new coreorder($coredata);
		$core->save();

		//创建订单商品列表并扣除库存
		ordergoods::createdata($data, $order_id);

		//删除购物车
		if (!empty($this->_context->cart_ids)) {
			$array = explode(",", $this->_context->cart_ids);
			$this->union_table = shopcart::meta();
			for ($i = 0; $i < count($array); $i++) {
				QDB::getConn()->startTrans();
				try {
					$this->union_table->deleteWhere(array("id" => $array[$i]));
					QDB::getConn()->completeTrans(true);
				} catch (Exception $e) {
					QDB::getConn()->completeTrans(FALSE);
				}
			}
		}
		$res = array("order_id" => $core->id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 我的订单
	 */
	function actionmyshoporder() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		//页码数
		$page = $this->_context->page ? $this->_context->page : 0;
		$limit = $this->_context->limit ? $this->_context->limit : 3;

		if (empty($this->_context->status)) {
			$res = shoporder::myorder($user_id, $page, $limit);
		} else {
			$res = shoporder::myorder($user_id, $page, $limit, $this->_context->status);
		}
		$this->responseMsg(0, $res);
	}

	/*
	 * 订单详情
	 */
	function actionorderdetail() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = shoporder::orderdetail($this->_context->order_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 订单物流详情
	 */
	function actionshipdetail() {
		$signCheck = $this->checkParamValid();
		//$user_id = $this->getuserid();
		$res = shoporder::ordershipdetail($this->_context->id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 确认收货
	 */
	function actionconfirmorder() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		if (empty($this->_context->order_id) || $this->_context->order_id == "undifined") {
			$this->responseMsg(1005);
		}
		$res = shoporder::confirmorder($this->_context->order_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 提交退款申请
	 */
	function actionorderrefund() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$refund_no = coreorder::getorderno();
		$data = array(
			"user_id" => $user_id,
			"img" => json_encode(explode(',', $this->_context->img)),
			"order_refund_no" => $refund_no,
			"refund_price" => $this->_context->price,
			"desc" => $this->_context->reason,
			"acct_id" => $this->acct_id,
			"order_id" => $this->_context->order_id
		);
		$refund = new shoporderrefund($data);
		$refund->save();
		//修改订单状态
		$order = shoporder::find("id = " . intval($this->_context->order_id))->getOne();
		$order->status = -1;
		$order->save();
		$this->responseMsg(0, $refund->id);
	}

	/*
	 * 删除订单
	 */
	function actiondelshoporder() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$order_id = $this->_context->order_id;
		$order = shoporder::find(array("id" => $order_id, "user_id" => $user_id))->getOne();
		if ($order->status == 3 || $order->status == 4) {
			$this->responseMsg(3002);
		}
		$order->is_delete = 1;
		$order->save();
		$this->responseMsg(0);
	}

	/*
	 * 取消订单
	 */
	function actioncancelshoporder() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$order_id = $this->_context->order_id;
		$order = shoporder::find(array("id" => $order_id, "user_id" => $user_id))->getOne();
		if ($order->status != 2) {
			$this->responseMsg(3003, '无法取消进行中的订单');
		}
		$order->status = 1;
		$order->save();
		$this->responseMsg(0);
	}

	/*
	 * 添加/删除商品收藏
	 */
	function actioncollectgoods() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$goods_id = $this->_context->goods_id;
		$collect = goodscollect::find(array(
			"goods_id" => $goods_id,
			"user_id" => $user_id
		))->getOne();
		if (empty($collect->id)) {
			$data = array(
				"goods_id" => $goods_id,
				"user_id" => $user_id,
				"acct_id" => $this->acct_id
			);
			$collect = new goodscollect($data);
			$collect->save();
			$this->responseMsg(0, ['status' => true]);
		} else {
			if ($collect->is_delete == 0) {
				$collect->is_delete = 1;
				$collect->save();
				$this->responseMsg(0, ['status' => false]);
			} else {
				$collect->is_delete = 0;
				$collect->save();
				$this->responseMsg(0, ['status' => true]);
			}
		}
	}

	/*
	 * 添加/删除商品收藏
	 */
	function actionmycollectgoods() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = goodscollect::mycollect($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 添加商品评论
	 */
	function actionaddgoodscomment() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$content = trim($this->_context->content);
		$img = $this->_context->img;
		if (empty($this->_context->ordergoods_id)) {
			$this->responseMsg(1005);
		}
		if (!empty($img)) {
			$img = json_encode(explode(",", $img));
		}
		$ordergoods = ordergoods::find("id = " . $this->_context->ordergoods_id)->getOne();
		$data = array(
			"acct_id" => $this->acct_id,
			"order_id" => $ordergoods->order_id,
			"goods_id" => $ordergoods->goods_id,
			"user_id" => $user_id,
			"content" => $content,
			"upload_img" => $img
		);
		$comment = new goodscomment($data);
		$comment->save();

		//修改订单商品评论状态
		// $order = shoporder::find("id = ".$this->_context->order_id)->getOne();
		// $order->comment_status = 1;
		// $order->save();
		$ordergoods->is_comment = 1;
		$ordergoods->save();
		$this->responseMsg(0, $comment->id);
	}

	/*
	 * 商品评论列表
	 */
	function actiongoodscommentlist() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$goods_id = $this->_context->goods_id;
		$res = goodscomment::getcommentlist($goods_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 地址列表
	 */
	function actionmyaddress() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = shopaddress::myaddress($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 增加/修改地址
	 */
	function actionaddaddress() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$area_arr = explode(",", trim($this->_context->areaInfo));
		if (empty($this->_context->id)) {
			$data = array(
				"user_id" => $user_id,
				"contact_name" => $this->_context->contact_name,
				"address" => $this->_context->address,
				"tel" => $this->_context->tel,
				"is_default" => intval($this->_context->is_default),
				"province" => $area_arr[0],
				"city" => $area_arr[1],
				"area" => $area_arr[2]
			);
			$address = new shopaddress($data);
			$address->save();
			$this->responseMsg(0, $address->id);
		} else {
			$address = shopaddress::find("id = " . $this->_context->id)->getOne();
			if ($this->_context->contact_name) {
				$address->contact_name = trim($this->_context->contact_name);
			}
			if ($this->_context->address) {
				$address->address = trim($this->_context->address);
			}
			if ($this->_context->tel) {
				$address->tel = trim($this->_context->tel);
			}
			if ($this->_context->areaInfo) {
				$address->province = $area_arr[0];
				$address->city = $area_arr[1];
				$address->area = $area_arr[2];
			}
			if ($this->_context->is_default) {
				$address->is_default = intval($this->_context->is_default);
			}
			$address->save();
			$this->responseMsg(0, $address->id);
		}
	}

	/*
	 * 地址详情
	 */
	function actionaddressinfo() {
		$signCheck = $this->checkParamValid();
		$res = shopaddress::find("id = " . $this->_context->id)->asArray()->getOne();
		$this->responseMsg(0, $res);
	}

	/*
	 * 删除地址
	 */
	function actiondeladdress() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$address = shopaddress::find(array("id" => $this->_context->id, "user_id" => $user_id))->getOne();
		$address->is_delete = 1;
		$address->save();
		$this->responseMsg(0, 1);
	}

	/*
	 * 题库首页
	 */
	function actionpaperindex() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$info = paper::getindexinfo($user_id);
		$info['auth'] = acct::checkversion($this->acct_id, $this->version);
		$this->responseMsg(0, $info);
	}

	/**
	 * 错题列表
	 * @Author   xy
	 * @DateTime 2019-10-28T10:27:12+0800
	 * @return   [type]                   [description]
	 */
	function actionmistakelist() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = papersubjectmistakes::getmistaklist($user_id, $this->_context->page);
		$this->responseMsg(0, $res);
	}

	/*
	 * 删除错题记录
	 */
	function actiondelmistake() {
		$signCheck = $this->checkParamValid();
		// $user_id = $this->getuserid();
		$id = $this->_context->id;
		if (empty($id)) {
			$this->responseMsg(1005);
		}
		// $res = papersubjectmistakes::delmistake($user_id,$paper_id);
		$res = papersubjectmistakes::delmistakeid($id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 科目列表
	 */
	function actionsubjectlist() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$type = $this->_context->type;
		$res = papersubject::getlist($this->acct_id, $type);
		$this->responseMsg(0, $res);
	}

	/*
	 * 科目下课程列表
	 */
	function actionsubjectcourse() {
		$signCheck = $this->checkParamValid();
		$sid = $this->_context->subject_id;
		//默认科目
		if (empty($sid)) {
			$subject = papersubject::find(array("acct_id" => $this->acct_id))->order("weight desc")->getOne();
			$sid = $subject->id;
		}
		$res = papersubject::getcourselist($sid);
		$this->responseMsg(0, $res);
	}

	/*
	 * 课程下章节列表
	 */
	function actioncoursechapter() {
		$signCheck = $this->checkParamValid();
		$cid = $this->_context->id;
		$res = papercoursechapter::getlist($cid);
		$this->responseMsg(0, $res);
	}

	function actionpaperchapterinfo() {
		$signCheck = $this->checkParamValid(false);
		$id = $this->_context->id;
		$res = papercoursechapter::getinfo($id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 试卷列表
	 */
	function actionpaperlist() {
		$signCheck = $this->checkParamValid();
		$id = $this->_context->id;
		$type = $this->_context->type;
		$auth = acct::checkversion($this->acct_id, $this->version);
		$res = paper::getlist($id, $type);
		if (!$auth) {
			for ($i = 0; $i < count($res); $i++) {
				$res[$i]['type'] = 1;
			}
		}
		$this->responseMsg(0, $res);
	}

	/*
	 * 试卷信息-问题列表
	 */
	function actionquestionlist() {
		$signCheck = $this->checkParamValid();
		$paper_id = $this->_context->paper_id;
		$user_id = $this->getuserid();
		if (!empty($this->_context->exam_id)) {
			$questions = paperquestion::questionlistexam($paper_id, $this->_context->exam_id, $user_id);
		} else {
			$questions = paperquestion::getlist($paper_id, $user_id);
		}
		$paper_info = paper::getpaperinfo($paper_id, $user_id);
		$res['paper_info'] = $paper_info;
		$res['questions'] = $questions;
		$res['AUTH'] = paper::checkUserAuth($paper_id, $user_id);
		$auth = acct::checkversion($this->acct_id, $this->version);
		if (!$auth) {
			$res['AUTH'] = true;
		}
		$this->responseMsg(0, $res);
	}

	/**
	 * 提交订单
	 * @Author   xy
	 * @DateTime 2019-10-16T10:25:03+0800
	 * @return   [type]                   [description]
	 */
	function actionaddpaperorder() {
		$signCheck = $this->checkParamValid();
		$paper_id = $this->_context->paper_id;
		$user_id = $this->getuserid();
		$res = paperorder::addOrder($paper_id, $user_id);
		$this->responseMsg(0, $res);
	}

	function actionaddpapershare() {
		$signCheck = $this->checkParamValid();
		$paper_id = $this->_context->paper_id;
		$user_id = $this->getuserid();
		$res = papershare::addShare($paper_id, $user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 交卷
	 */
	function actionsubmitpaper() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$paper_data = $this->_context->paper_data;
		$start_do = $this->_context->startdo_time;
		$res = paperquestion::submitpaper($paper_data, $user_id, $this->acct_id, $start_do, $this->_context->type);
		$this->responseMsg(0, $res);
	}

	//交卷报告
	function actionexamreport() {
		$signCheck = $this->checkParamValid();
		$exam_id = $this->_context->exam_id;
		$dolog = paperexam::examdolog($exam_id);
		$report = paperexam::examreport($exam_id);
		$res['dolog'] = $dolog;
		$res['report'] = $report;
		$this->responseMsg(0, $res);
	}

	function actionpapersubjectinfo() {
		$signCheck = $this->checkParamValid(false);
		$id = $this->_context->id;
		$res = papersubject::find("id=?", $id)->getOne();
		if ($res->id()) {
			$this->responseMsg(0, $res->toArray());
		}
		$res = new papersubject();
		$this->responseMsg(0, $res->toArray());
	}

	function actionpapercourseinfo() {
		$signCheck = $this->checkParamValid(false);
		$id = $this->_context->id;
		$res = papercourse::find("id=?", $id)->getOne();
		if ($res->id()) {
			$this->responseMsg(0, $res->toArray());
		}
		$res = new papercourse();
		$this->responseMsg(0, $res->toArray());
	}

	/*
	 * 做题记录科目列表
	 */
	function actionsubjectdolog() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = paperquestiondolog::getsubjectdolog($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 做题记录科目下试题列表
	 */
	function actionpaperdolog() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$subject_id = $this->_context->subject_id;
		$res = paperquestiondolog::getpaperdolog($subject_id, $user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 收藏问题
	 */
	function actioncollectpaper() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$paper_id = $this->_context->paper_id;
		if (empty($paper_id)) {
			$this->responseMsg(1005);
		}
		$res = papersubjectcollection::collectpaper($user_id, $paper_id, $this->_context->question_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 我收藏的科目列表
	 */
	function actionmycollectsubject() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = papersubjectcollection::mycollectsubject($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 我收藏的科目下试卷列表
	 */
	function actionmycollectpaper() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		// $subject_id = $this->_context->subject_id;
// 		if(empty($subject_id)){
// 			$this->responseMsg(1005);
// 		}
		$res = papersubjectcollection::mycollecttest($user_id);
		$this->responseMsg(0, $res);
	}

	function actionquestiondetail() {
		$signCheck = $this->checkParamValid();
		$paper_id = $this->_context->paper_id;
		$user_id = $this->getuserid();
		$questions = paperquestion::getlistforid($paper_id, $user_id, $this->_context->question_id, $this->_context->dolog_id);
		// $paper_info = paper::getpaperinfo($paper_id,$user_id);
		$res['paper_info'] = [];
		$res['questions'] = $questions;
		$this->responseMsg(0, $res);
	}

	/*
	 * 做题记录科目列表
	 */
	function actionmysubjectdolog() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = paperquestiondolog::getsubjectdolog($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 做题记录科目下试卷列表
	 */
	function actionmypaperdolog() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$subject_id = $this->_context->subject_id;
		if (empty($subject_id)) {
			$this->responseMsg(1005);
		}
		$res = paperquestiondolog::getpaperdolog($user_id, $subject_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 错题记录科目列表
	 */
	function actionmysubjectmistake() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = papersubjectmistakes::getsubjectmistake($user_id);
		$this->responseMsg(0, $res);
	}

	/*
	 * 错题记录科目下试卷列表
	 */
	function actionmypapermistake() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$subject_id = $this->_context->subject_id;
		if (empty($subject_id)) {
			$this->responseMsg(1005);
		}
		$res = papersubjectmistakes::getpapermistake($user_id, $subject_id);
		$this->responseMsg(0, $res);
	}

	/**
	 * 搜索试卷
	 * @Author   xy
	 * @DateTime 2020-02-18T12:09:13+0800
	 * @return   [type]                   [description]
	 */
	function actionpapersearch() {
		$signCheck = $this->checkParamValid();
		$res = paper::search($this->_context->keyword);
		$this->responseMsg(0, $res);
	}

	/**
	 * 我的测验记录
	 * @Author   xy
	 * @DateTime 2019-10-29T16:59:14+0800
	 * @return   [type]                   [description]
	 */
	function actionmyexam() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = paperexam::myExam($user_id);
		$this->responseMsg(0, $res);
	}

	/**
	 * 更新用户信息
	 * @Author   xy
	 * @DateTime 2020-02-18T11:57:15+0800
	 * @return   [type]                   [description]
	 */
	function actionupdateuserinfo() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$user = acctuser::find('id=?', $user_id)->getOne();
		if ($user->id()) {
			if (!empty($this->_context->real_name)) {
				$user->real_name = $this->_context->real_name;
			}
			if (!empty($this->_context->id_card_num)) {
				$user->id_card_num = $this->_context->id_card_num;
			}
			$user->save();
		}
		$this->responseMsg(0, $user->toArray());
	}


	//申请直播
	function actionapplylive() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();

		$info = liveapply::find(array("user_id" => $user_id))->getOne();

		if (empty($this->_context->tel)) {
			$this->appErrCode['2006'] = '主播手机号码必须填写';
			$this->responseMsg(2006);
		}
		if (empty($this->_context->apply_name)) {
			$this->appErrCode['2006'] = '申请人姓名必须填写';
			$this->responseMsg(2006);
		}
		if (empty($this->_context->apply_avatar)) {
			$this->appErrCode['2006'] = '主播头像必须上传';
			$this->responseMsg(2006);
		}
		if (empty($this->_context->title)) {
			$this->appErrCode['2006'] = '直播间标题必须填写';
			$this->responseMsg(2006);
		}

		if (empty($this->_context->pic)) {
			$this->appErrCode['2006'] = '封面图片必须上传';
			$this->responseMsg(2006);
		}

		if (empty($this->_context->dec)) {
			$this->appErrCode['2006'] = '直播室简介必须填写';
			$this->responseMsg(2006);
		}


		$apply_name = $this->_context->apply_name;
		$title = $this->_context->title;
		$tel = $this->_context->tel;
		$price = $this->_context->price;
		$type = $this->_context->type;
		$avatar = $this->_context->apply_avatar;
		$dec = $this->_context->dec;
		$pic = $this->_context->pic;
		//	var_dump($this->_context);die;
		if ($info->id()) {
			$info->apply_name = $apply_name;
			$info->title = $title;
			$info->tel = $tel;
			$info->price = $price;
			$info->type = $type;
			$info->apply_avatar = $avatar;
			$info->dec = $dec;
			$info->pic = $pic;
			$info->save();
		} else {
			$data = array(
				"apply_name" => $apply_name,
				"title" => $title,
				"tel" => $tel,
				"price" => $price,
				"type" => $type,
				"dec" => $dec,
				"user_id" => $user_id,
				"acct_id" => $this->acct_id,
				"pic" => $pic,
				"apply_avatar" => $avatar
			);
			$info = new liveapply($data);
			$info->save();
		}
		//主播信息
		$info->status = 1;
		$info->save();
		$teacher = teacher::find('user_id = ?', $user_id)->getOne();
		if (!$teacher->id()) {
			//初始化主播信息
			$teacher = new teacher();
		}
		$teacher->acct_id = $this->acct_id;
		$teacher->name = $apply_name;
		$teacher->user_id = $user_id;
		$teacher->icon = $avatar;
		$teacher->intro = $dec;
		$teacher->updatetime = time();
		$teacher->save();

		$teacherid = $teacher->id;
		$bindlive = video::find('teacher_id = ?', $teacherid)->getOne();
		if ($bindlive->id()) {//提交之后已经上架的直播间需要下架
			$bindlive->status = 0;
			$bindlive->save();
		}


		$this->appErrCode[0] = '提交成功，请等待审核';
		$this->responseMsg(0);

	}

	//直播申请详情
	function actionliveapplydetail() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = liveapply::find(array("user_id" => $user_id))->getOne();
		if (!$res->id()) {
			$res = array();
			$res['apply_name'] = '';
			$res['apply_msg'] = '';
			$res['apply_avatar'] = '';
			$res['dec'] = '';
			$res['pic'] = '';
			$res['tel'] = '';
			$res['title'] = '';
			$res['type_index'] = 0;
			$res['category'] = educategory::getselectparentlist($this->acct_id);
		} else {
			$res = $res->toArray();
			$res['category'] = educategory::getselectparentlist($this->acct_id);
			for ($i = 0; $i < count($res['category']); $i++) {
				if ($res['category'][$i]['id'] == $res['type']) {
					$res['type_index'] = $i;
				}
			}
		}
		$this->responseMsg(0, $res);
	}

	/*
     *添加直播拉流配置信息。
     */
	function actionmyliveroom() {
		$signCheck = $this->checkParamValid();//要求必须登录获取用户信息
		$user = acctuser::find(array("wechat_open_id" => $this->openid))->getOne();
		if (!$user->id()) {
			$this->responseMsg(2004);
		}
		$res = helper_liveroom_url::checkpush($this->acct_id, $user->id, $this->openid);
		if (is_array($res)) {
			if ($res['code'] == 1) {//抛错
				$this->appErrCode['2006'] = $res['msg'];
				$this->responseMsg(2006);
			} else if ($res['code'] == 400) {
				$this->appErrCode['2006'] = $res['msg'];
				$this->responseMsg(2006);
			} else {
				$teacher = teacher::find(array("user_id" => $user->id))->getOne();
				$teacher->toArray();
				if ($teacher->id()) {


					//判断当前主播是否绑定了直播间
					$liveroom = video::find('teacher_id = ?', $teacher->id)->order('id desc')->getOne();

					if (!$liveroom->id()) {
						$this->appErrCode['2006'] = '当前主播审核中或尚未绑定直播间';
						$this->responseMsg(2006);
					}
					if ($liveroom->status == 0) {
						$this->appErrCode['2006'] = '当前直播间已经下架';
						$this->responseMsg(2006);

					}

					$liveroom->live_url = $teacher->playurl;

					if ($liveroom->end_time < time() + 24 * 3600) {//确保主播进去没有过期
						$liveroom->end_time = time() + 24 * 3600;
					}
					$liveroom->save();

					$teacher->content = htmlspecialchars_decode($teacher->content);
					$result = $teacher->toArray();
					$result['liveurl'] = 'showpusher=1&id=' . $liveroom->id . "&sourceurl=" . urlencode($result['sourceurl']);
					$this->responseMsg(0, $result);
				} else {
					$this->appErrCode['2006'] = '当前用户不是主播';
					$this->responseMsg(2006);
				}
			}
		} else {
			$this->appErrCode['2006'] = '主播初始化推流信息异常';
			$this->responseMsg(2006);
		}
	}

	function actionaddlivecomment() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$content = trim($this->_context->content);
		$img = $this->_context->img;
		// if(empty($this->_context->id)){
		// 	$this->responseMsg(1005);
		// }
		// if(!empty($img)){
		// 	$img = json_encode(explode(",", $img));
		// }
		$acct = acct::find('id=1')->getOne();
		if ($acct->id()) {
			if ($content) {
				$safe_res = Helper_Common::wxsafeMsg($acct->app_id, $acct->app_secret, $content, 'msg');
				if (!empty($safe_res['errcode']) && $safe_res['errcode'] == 87014) {
					$this->appErrCode[2006] = "发送失败，内容包含敏感信息！";
					$this->responseMsg(2006);
				}
			}

			$res = comment::addComment($user_id, $this->_context->id, $content, $img, $this->acct_id, $acct->aodian_accessid, $acct->aodian_accesskey);
			$this->responseMsg(0, $res);
		}

	}

	/**
	 * 奥点云加入直播间
	 * @Author   xy
	 * @DateTime 2020-03-09T17:36:30+0800
	 * @return   [type]                   [description]
	 */
	function actionjoinlive() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$video_id = $this->_context->id;
		$acct = acct::find('id=?', $this->acct_id)->getOne();
		$domain = $acct->app_id . '_' . $video_id;
		$api = new Helper_TisApi($acct->aodian_accessid, $acct->aodian_accesskey, $acct->aodian_auth);
		//获取用户列表
		$host = Helper_Common::getRequestDomain();
		$filename = Q::ini('app_config/ROOT_DIR') . '/config/redis.yaml';
		$config = Helper_YAML::load($filename);
		$redis = new Redis();
		$redis->pconnect($config['host'], $config['port']);
		$user_count = $redis->get("edulive:$host:liveroom:user:" . $video_id);
		if ($redis->exists("edulive:$host:liveroom:expire:" . $video_id) && $user_count) {
			$sum = $user_count + 1;
			$redis->set("edulive:$host:liveroom:user:" . $video_id, $sum);
		} else {
			$number = $api->getTopicNum($domain);

			$sum = $number['total'];
			$redis->set("edulive:$host:liveroom:user:" . $video_id, $sum);
			$redis->expire("edulive:$host:liveroom:expire:" . $video_id, 20);
			$redis->set("edulive:$host:liveroom:expire:" . $video_id, 1);
			$redis->expire("edulive:$host:liveroom:expire:" . $video_id, 10);
			// var_dump($sum);
		}
		$user_info = acctuser::getuserinfo($user_id);
		$video = video::find('id=?', $video_id)->getOne();
		// if($video->id())
		$param = array(
			'body' => json_encode(array(
				'video_id' => $video_id,
				'join_user' => empty($acct->show_join_user) ? [] : array(
					"id" => $user_id,
					"nickname" => !empty($user_info['username']) ? $user_info['username'] : $user_info['nickname'],
					"avatar_url" => $user_info['avatar_url'],
					'type' => 'join',
				),
				'live_user' => $video->id() ? $video->inliveuser + $sum : $sum,
			))
		);


		$res = $api->sendMsg($param, $domain);
		$this->responseMsg(0, $res);
	}

	/**
	 * 熬点云断联
	 * @Author   xy
	 * @DateTime 2019-12-27T15:58:01+0800
	 * @return   [type]                   [description]
	 */
	function actioncloseconnect() {
		$signCheck = $this->checkParamValid();
		$userid = $this->getuserid();

		$video_id = intval($this->_context->id);
		$host = Helper_Common::getRequestDomain();
		$filename = Q::ini('app_config/ROOT_DIR') . '/config/redis.yaml';
		$config = Helper_YAML::load($filename);
		$redis = new Redis();
		$redis->pconnect($config['host'], $config['port']);
		$user_count = $redis->get("edulive:$host:liveroom:user:" . $video_id);
		if ($user_count) {
			$sum = $user_count - 1;
			$redis->set("edulive:$host:liveroom:user:" . $video_id, $sum);
		} else {
			$acct = acct::find('id=?', $this->acct_id)->getOne();
			$domain = $acct->app_id . '_' . $video_id;
			$api = new Helper_TisApi($acct->aodian_accessid, $acct->aodian_accesskey, $acct->aodian_auth);
			$number = $api->getTopicNum($domain);
			$sum = $number['total'];
			$redis->set("edulive:$host:liveroom:user:" . $video_id, $sum);
			$redis->expire("edulive:$host:liveroom:user:" . $video_id, 10);
			// var_dump($sum);
		}
		// $video = video::find('id=?',$video_id)->setColumns('id,inliveuser')->getOne();

		// $number = $api->getTopicNum($domain);
		$msg = array(
			"video_id" => $video_id,
			"live_user" => $sum,
			"join_user" => array()
		);
		$api->sendMsg(array('body' => $msg), $domain);
		$this->responseMsg(0, $res);

	}

	/**
	 * 获取直播间在线人数
	 * @Author   xy
	 * @DateTime 2019-12-27T16:21:18+0800
	 * @return   [type]                   [description]
	 */
	function actiongetliveusernumber() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$video_id = intval($this->_context->id);
		$acct = acct::find('id=?', $this->acct_id)->getOne();
		$domain = $acct->app_id . '_' . $video_id;
		$api = new Helper_TisApi($acct->aodian_accessid, $acct->aodian_accesskey, $acct->aodian_auth);
		$number = $api->getTopicNum($domain);
		$this->responseMsg(0, $number);
	}

	/**
	 * 视频题目信息
	 * @Author   xy
	 * @DateTime 2020-03-17T20:03:02+0800
	 * @return   [type]                   [description]
	 */
	function actiongetvideoquestion() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = videoquestion::getList($this->_context->id, $user_id);
		$this->responseMsg(0, $res);
	}

	function actionsubmitvideoquestion() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$chapter_id=$this->_context->chapter_id;
		$res = videoquestion::submitpaper($this->_context->info, $this->_context->video_id, $this->_context->chapter_id, $user_id);

		$edu_video_exam_sql = 'select * from edu_video_exam where user_id = ' . $user_id . ' and video_id = ' . $this->_context->video_id. ' group by chapter_id ';
		$edu_video_exam_data = QDB::getConn()->execute($edu_video_exam_sql)->fetchAll();
		$exam_num = count($edu_video_exam_data);
		$edu_video_chapter_sql = 'select * from edu_video_chapter where video_id = ' . $this->_context->video_id;
		$edu_video_chapte_data = QDB::getConn()->execute($edu_video_chapter_sql)->fetchAll();
		$chapter_num = count($edu_video_chapte_data);

		$schedule = round($exam_num / $chapter_num, 2);
		$learnschedule = learnschedule::find('uid = ? and video_id = ?', $user_id, $this->_context->video_id)->getOne();
		$is_videoexam = videoexam::find('user_id = ? and chapter_id = ? and video_id = ?', $user_id, $chapter_id,$this->_context->video_id)->getOne();

		if ($learnschedule->id() == '') {
			$learnschedule = new learnschedule();
		} else {
			$learnschedule = learnschedule::find('uid = ? and video_id = ?', $user_id, $this->_context->video_id)->getOne();
		}
		$learnschedule->video_id = $this->_context->video_id;
		$learnschedule->schedule = $schedule;
		$learnschedule->uid = $user_id;

		$learnschedule->save();
		$this->responseMsg(0, $res);
	}

	/*
	 * 分享成功
	 */
	function actionsharesuccess() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$video_id = $this->_context->video_id;
		if (empty($video_id)) {
			$this->responseMsg(1005);
		} else {
			$res = videosharelog::addlog($user_id, $video_id);
			$this->responseMsg(0, $res);
		}
	}

	/*
	 * 获取分享信息
	 */
	function actionshareinfo() {

	}

	/*
	 * 关注图片
	 */
	function actionguanzhu() {
		$set = pubconfig::getValue('sub_public_img');
		if ($set) {
			$this->responseMsg(0, $set);
		}
		$url = Helper_Common::getRequestHost();
		$this->responseMsg(0, $url . '/img/guanzhu-gzh.jpg');
	}

	function actioncity() {
		$res = Helper_City::wxapp();
		$this->responseMsg(0, $res);
	}

	/*
	 * 获取分享二维码
	 */
	function actionshareqrcode() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$res = acctuser::getQrcode($this->acct_id, $user_id);
		if ($res['code'] == 0) {
			//获取背景图片
			$set = fxset::getfxset($this->acct_id);
			$res['background_img'] = $set->bg_img;
			$res['dec'] = $set->sysset;
		}
		$this->responseMsg(0, $res);
	}

	/**
	 * 视频二维码
	 * @Author   xy
	 * @DateTime 2020-01-04T11:20:43+0800
	 * @return   [type]                   [description]
	 */
	function actionvideoqrcode() {

		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$id = $this->_context->id;
		$video = video::find('id=?', $id)->getOne();
		if (!$video->id()) {
			$this->responseMsg(1005);
		}
		$path = "pages/course-detail/course-detail";
		if ($video->style == 3) {
			$path = "pages/live-class/live-class";
		}
		$res = acctuser::getQrcode($this->acct_id, $user_id, $path, "pid=$user_id&video_id=$id");
		if ($res['code'] == 0) {
			//获取用户头像
			//$res['qr_img'] = Helper_Common::getRequestHost()."img/user.png";
			$user_img = acctuser::find("id=?", $user_id)->getOne()->avatar_url;
			$path = acctuser::saveTempImage($user_img);
			$res['headimg'] = Helper_Common::getJiqunHost() . $path;
		}
		$this->responseMsg(0, $res);
	}

	/**
	 * 商品二维码
	 * @Author   xy
	 * @DateTime 2020-01-04T11:20:43+0800
	 * @return   [type]                   [description]
	 */
	function actiongoodsqrcode() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$id = $this->_context->id;
		$res = acctuser::getQrcode($this->acct_id, $user_id, 'pages/goods-details/goods-details', "pid=$user_id&goods_id=$id");
		if ($res['code'] == 0) {
			//获取用户头像
			//$res['qr_img'] = Helper_Common::getRequestHost()."img/user.png";
			$user_img = acctuser::find("id=?", $user_id)->getOne()->avatar_url;
			$path = acctuser::saveTempImage($user_img);
			$res['headimg'] = Helper_Common::getRequestHost() . $path;
		}
		$this->responseMsg(0, $res);
	}

	function actionteamset() {
		$signCheck = $this->checkParamValid();
		$set = fxset::getfxset($this->acct_id);
		$this->responseMsg(0, $set->level);
	}

	//关于我们
	function actionabouts() {
		$signCheck = $this->checkParamValid(false);
		$res = acct::find("id = ?", $this->acct_id)->getOne();
		$this->responseMsg(0, $res->dec ? $res->dec : '暂无信息');
	}


	function getAgentId() {
		if (isset($this->_packageid) && intval($this->_packageid)) {
			return intval($this->_packageid);
		} else {
			return 2;
		}
	}


	function actiontest() {
		$res = videoreward::testpayok($this->_context->id);
		$this->responseMsg(0, $res);
	}


	//图片上传
	function actionUpload() {
		$allowExt = 'jpg,png,gif,bmp,swf';
		$upFolder = 'uploadfiles/image/';
		if ($this->_context->isPOST()) {
			$errorString = '';
			# 图片上传
			$uploader = new Helper_Uploader();
			if ($uploader->filesCount()) {
				$uploader = new Helper_Uploader();
				$upfile = $uploader->file('image');
				$extname = $upfile->extname();# 扩展名
				$old_name = $upfile->filename();
				if ($upfile->isSuccessed()) {
					if ($upfile->isValid($allowExt)) {
						$filename = md5($old_name . time()) . '.' . $extname;
						$filepath = $upFolder . $filename;
						# 移动文件
						$upfile->move($filepath);
						if (!$upfile->isMoved()) {
							$errorString = '图片上传失败!请检查服务器配置!';

						} else {
							$errorString = '图片上传成功!';
							$url = Helper_Common::getJiqunHost();
							$data['url'] = $url . $filepath;
							$this->responseMsg(0, $data);
							//$this->_view['filepath'] = $filepath;
						}
					} else {
						$errorString = '图片上传失败!图片格式不正确!';
					}
				} else {
					$errorString = '图片上传失败!请检查服务器配置!';
				}
				$this->responseMsg(0, $errorString);
			}

		}
	}

	//定时任务
	function actionruntimerole() {
		//开课提醒
		$time = time() - 3600;
		$stime = time();
		$sql = "select id,acct_id FROM edu_video where status = 1 and style = 3 and is_send = 0 and opening_time between $stime and $time";
		$list = QDB::getConn()->execute($sql)->fetchAll();
		for ($i = 0; $i < count($list); $i++) {
			$video = video::find("id =?", $list[$i]['id'])->getOne();
			$video->is_send = 1;
			$video->save();
			video::startlivemsg($list[$i]['acct_id'], $list[$i]['id']);
		}
	}

	function actionallclass() {
		$educategory_sql = "select * from edu_category where status=1 order by weight desc";
		$educategory = QDB::getConn()->execute($educategory_sql)->fetchAll();
		if (empty($educategory)) return $this->responseMsg(1);
		$first_class = educlass::find('cate_id = ?', $educategory[0]['id'])->asArray()->getAll();
		if (empty($first_class)) return $this->responseMsg(1);
		$all_class = educlass::find()->asArray()->getAll();
		if (empty($all_class)) return $this->responseMsg(1);
		$data = ['cate' => $educategory, 'first_class' => $first_class, 'all_class' => $all_class];
		$this->responseMsg(0, $data);
	}

	//提交预约
	function actionyuyue() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$company_name = $this->_context->company_name;
		$user_name = $this->_context->user_name;
		$user_tel = $this->_context->user_tel;
		$user_email = $this->_context->user_email;
		$content = $this->_context->content;
		$inser = [
			'company_name' => $company_name,
			'user_name' => $user_name,
			'user_tel' => $user_tel,
			'user_email' => $user_email,
			'content' => $content,
			'uid' => $user_id,
		];
		$yuyue = new yuyue($inser);
		$yuyue->save();
		$this->responseMsg(0, '预约成功');
	}

	//编辑发票
	function actioninvoice() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$invoice = invoice::find('uid = ?', $user_id)->getOne();
		if ($invoice->id() == '') {
			$invoice = new invoice();
		} else {
			$invoice = invoice::find('uid = ?', $user_id)->getOne();
		}
		$acctuser = acctuser::find('id = ?', $user_id)->asArray()->getOne();
		$address = $this->_context->address;
		$bank = $this->_context->bank;
		$bank_card = $this->_context->bank_card;
		$start = $this->_context->start;
		$tax_id = $this->_context->tax_id;
		$tel = $this->_context->tel;

		$invoice->start = $start;
		$invoice->tax_id = $tax_id;
		$invoice->address = $address;
		$invoice->tel = $tel;
		$invoice->bank = $bank;
		$invoice->bank_card = $bank_card;
		$invoice->uid = $user_id;
		$invoice->nickname = $acctuser['username'];

		$invoice->save();
		$this->responseMsg(0, '编辑成功');
	}

	//展示我的发票
	function actionshowmyinvoice() {
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$invoice = invoice::find('uid = ?', $user_id)->asArray()->getOne();
		if (empty($invoice)) {
			$this->responseMsg(1, '');
		} else {
			$this->responseMsg(0, $invoice);
		}
	}

//	//月报
//	function actiongetmonthlyreport() {
//		$acct = acct::find('id = 1')->asArray()->getOne();
//		$signCheck = $this->checkParamValid();
//		$user_id = $this->getuserid();
//		$userinfo = acctuser::find('id = ?', $user_id)->asArray()->getOne();
//		$username = $userinfo['username'];//微信名
//		$avatar_url = imagecreatefromstring(file_get_contents($userinfo['avatar_url']));//头像
//		$time = intval((time() - $userinfo['addtime']) / 60 / 60 / 24);//多少天
//		$qinshang = 100;//勤商
//		$now_time = strtotime(date('Y-m', time()) . '-1 0:0:0');
//		$video_num = videoexam::find('user_id = ? and created > ?', $user_id, $now_time)->getCount('id');//本月学习课程数
//		$paper = paper::getindexinfo($user_id);
//		$avg_right_per = $paper['avg_right_per'];//平均正确率
//		$applet_code = imagecreatefromstring(file_get_contents(self::actionyuanimg($acct['applet_code'])));//二维码
//		$background_image = imagecreatefromstring(file_get_contents($acct['background_image']));//背景图
//
//		$white = imagecolorallocate($background_image, 255, 255, 255);
//		$font = __DIR__ . '/msyh.ttc';
//		imagecopyresampled($background_image, $avatar_url, 102, 123, 0, 0, 312, 312, imagesx($avatar_url), imagesy($avatar_url));
//		imagettftext($background_image, 50, 0, 500, 300, $white, $font, '我是');
//		imagettftext($background_image, 50, 0, 640, 300, $white, $font, $username);
//		imagettftext($background_image, 50, 0, 200, 680, $white, $font, '你已经来到优也树');
//		imagettftext($background_image, 50, 0, 820, 680, $white, $font, $time);
//		imagettftext($background_image, 50, 0, 952, 680, $white, $font, '天了');
//		imagettftext($background_image, 50, 0, 200, 780, $white, $font, '很高兴看到你在学习中进步');
//		imagettftext($background_image, 50, 0, 200, 880, $white, $font, '为企业奉献自己的力量');
//		imagettftext($background_image, 50, 0, 200, 1080, $white, $font, '你的勤商是<');
//		imagettftext($background_image, 50, 0, 600, 1080, $white, $font, $qinshang . '+');
//		imagettftext($background_image, 50, 0, 200, 1180, $white, $font, '你是最优秀的');
//		imagettftext($background_image, 50, 0, 200, 1380, $white, $font, '这月你学习了');
//		imagettftext($background_image, 50, 0, 650, 1380, $white, $font, $video_num);
//		imagettftext($background_image, 50, 0, 700, 1380, $white, $font, '门课程');
//		imagettftext($background_image, 50, 0, 200, 1480, $white, $font, '课后小练习正确率');
//		imagettftext($background_image, 50, 0, 750, 1480, $white, $font, $avg_right_per . '%');
//		imagettftext($background_image, 50, 0, 200, 1680, $white, $font, '你学习提升的一小步');
//		imagettftext($background_image, 50, 0, 200, 1780, $white, $font, '是企业成功的一大步');
//		imagecopyresampled($background_image, $applet_code, 400, 1900, 0, 0, 300, 300, imagesx($applet_code), imagesy($applet_code));
//		$imgurl='poster/'.time().'.jpeg';
//		imagejpeg($background_image,'./'.$imgurl);
//		$data=Helper_Common::getRequestHost().$imgurl;
//		$this->responseMsg(0,$data);
//	}

	//月报
	function actiongetmonthlyreport() {
		$acct = acct::find('id = 1')->asArray()->getOne();
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$userinfo = acctuser::find('id = ?', $user_id)->asArray()->getOne();
		$username = $userinfo['username'];//微信名
		$time = intval((time() - $userinfo['addtime']) / 60 / 60 / 24);//多少天
		$video_num = videoexam::find('user_id = ?', $user_id)->getCount('id');//我的视频数
		$company_num=acctuser::find('company_id = ?', $userinfo['company_id'])->asArray()->getAll();
		$com_user=[];
		for($i=0;$i<count($company_num);$i++){
			$com_user[$i]=intval($company_num[$i]['id']);
		}
		$users_vnum=[];
		for($i1=0;$i1<count($com_user);$i1++){
			$edu_video_exam_sql = 'select * from edu_video_exam where user_id = ' . $com_user[$i1] .' group by chapter_id ';
			$edu_video_exam_data = QDB::getConn()->execute($edu_video_exam_sql)->fetchAll();
//			$users_vnum_['num'] = videoexam::find('user_id = ?', $com_user[$i1])->getCount('id');
			$users_vnum_['num'] = count($edu_video_exam_data);
			$users_vnum_['id']=$com_user[$i1];
			array_push($users_vnum,$users_vnum_);
		}
		$last_names = array_column($users_vnum,'num');
		array_multisort($last_names,SORT_DESC,$users_vnum);
		$aaa=0;
		foreach ($users_vnum as $k=>$v){
			if($v['id']==intval($user_id)){
				$aaa= $k;
			}
		}
		$my_v_sort=intval($aaa)+1;//我的企业学习课程数排名
		$qinshang = intval($my_v_sort/count($company_num)*40)+60;//勤商
//		$paper = paper::getindexinfo($user_id);
		$ringht_num=videoquestiondolog::find("user_id = ? and isright=1",$user_id)->getAll();
		$question_num=videoquestiondolog::find("user_id = ?",$user_id)->getAll();
		if(!empty($question_num->id())&&!empty($ringht_num->id())){
			$rightlv=round(count($ringht_num)/count($question_num),2)*100;
		}else{
			$rightlv=0;
		}
		$avg_right_per = $rightlv;//平均正确率

		$data['name']=$username;
		$data['time']=$time;
		$data['qinshang']=$qinshang;
		$data['avg_right_per']=$avg_right_per;
		$data['rank']=$my_v_sort;
		$this->responseMsg(0,$data);
	}


	static function actionyuanimg($imgpath) {
		$ext = pathinfo($imgpath);
		$src_img = null;
		switch ($ext['extension']) {
			case 'jpg':
				$src_img = imagecreatefromjpeg($imgpath);
			break;
			case 'png':
				$src_img = imagecreatefrompng($imgpath);
				break;
		}
		$wh = getimagesize($imgpath);
		$w = $wh[0];
		$h = $wh[1];
		$w = min($w, $h);
		$h = $w;
		$img = imagecreatetruecolor($w, $h);
		//这一句一定要有
		imagesavealpha($img, true);
//拾取一个完全透明的颜色,最后一个参数127为全透明
		$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
		imagefill($img, 0, 0, $bg);
		$r = $w / 2; //圆半径
		$y_x = $r; //圆心X坐标
		$y_y = $r; //圆心Y坐标
		for ($x = 0; $x < $w; $x++) {
			for ($y = 0; $y < $h; $y++) {
				$rgbColor = imagecolorat($src_img, $x, $y);
				if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
					imagesetpixel($img, $x, $y, $rgbColor);
				}
			}
		}
		$imgurl='yuan/'.time().'.png';
		imagepng($img,'./'.$imgurl);
		return Helper_Common::getRequestHost().$imgurl;
	}

	function actiongetcompany(){
		$company=company::find('status = 1')->asArray()->getAll();
		$data = [];
		for($i=0;$i<count($company);$i++){
			$data[$i]['id']=$company[$i]['id'];
			$data[$i]['company_name']=$company[$i]['company_name'];
		}
		$this->responseMsg(0,$data);
	}

	function actionverifypassword(){
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$password=$this->_context->password;
		$bumen=$this->_context->bumen;
		$name=$this->_context->name;
		$cid=$this->_context->cid;
		$company=company::find('id = ?',$cid)->asArray()->getOne();
		$num = count(acctuser::find('company_id = ?',$cid)->asArray()->getAll());
		$acctuser = acctuser::find('id=?',$user_id)->getOne();
		$acctuser->company_id = $cid;
		$acctuser->department = $bumen;
		$acctuser->save();//编辑
		return $this->responseMsg(1,'验证通过');
//		if($company['review_password']!=trim($password)){
//			return $this->responseMsg(0,'密码错误');
//		}else{
//			$num = count(acctuser::find('company_id = ?',$cid)->asArray()->getAll());
//			if($num>=intval($company['authority'])){
//				return $this->responseMsg(0,'人数已达上限');
//			}else{
//				$acctuser = acctuser::find('id=?',$user_id)->getOne();
//				$acctuser->company_id = $cid;
//				$acctuser->department = $bumen;
//				$acctuser->save();//编辑
//				return $this->responseMsg(1,'验证通过');
//			}
//		}
	}

	function actionshowmycompany(){
		$signCheck = $this->checkParamValid();
		$user_id = $this->getuserid();
		$company = company::find('uid = ?', $user_id)->asArray()->getOne();
		$acctuser=acctuser::find('company_id = ?', $company['id'])->asArray()->getAll();
		if(!empty($acctuser)){
			return $this->responseMsg(0,$acctuser);
		}else{
			return $this->responseMsg(1,'没有成员');
		}

	}


}





